package com.nj9you.sdk.request;

import android.content.Context;

import com.nj9you.sdk.constant.Constants;
import com.nj9you.sdk.framework.ResponseCallback;
import com.nj9you.sdk.http.HttpCallback;
import com.nj9you.sdk.http.IHttp;
import com.nj9you.sdk.http.Response;
import com.nj9you.sdk.http.impl.HttpImpl;
import com.nj9you.sdk.model.PayInfo;
import com.nj9you.sdk.model.SdkInfo;
import com.nj9you.sdk.params.PayParams;
import com.nj9you.sdk.user.UserManager;
import com.nj9you.sdk.utils.Utils;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * 支付请求类
 */

public class PayRequestImpl implements IPayRequest {

    private Context mContext;
    private IHttp mHttp;

    public PayRequestImpl(Context context) {
        this.mContext = context;
        mHttp = new HttpImpl(context);
    }

    @Override
    public void queryPayList(Map<String, String> gameParams, ResponseCallback<List<PayInfo>> callback) {
    }

    @Override
    public void queryAlipayArgs(PayParams payParams, final ResponseCallback<JSONObject> callback) {
        if (payParams == null) {
            if (callback != null) {
                Response<JSONObject> response = new Response<JSONObject>();
                response.setCode(String.valueOf(-1));
                response.setMsg(mContext.getString(Utils.getStringId(mContext, "jy_null_payparams")));
                callback.onCallback(response);
            }
            return;
        }
        JSONObject jobj = new JSONObject();
        String channelId = SdkInfo.get(mContext).getChannel();
        String appId = SdkInfo.get(mContext).getAppId();
        String userId = UserManager.get(mContext).loadCurrentUser().getId();
        String server = payParams.getServer();
        int payType = 1;
        String orderId = payParams.getOrder();
        String subject = payParams.getSubject();
        String itemid = payParams.getItemid();
        String body = payParams.getBody();
        String attach = payParams.getAttach();
        String amount = payParams.getPrice();
        String signType = "MD5";
        String appKey = Constants.PAY_KEY;
        String sign = Utils.string2MD5(channelId + appId + userId + payType + amount + appKey);

        Utils.putJson(jobj, "channel", channelId);
        Utils.putJson(jobj, "appid", appId);
        Utils.putJson(jobj, "uid", userId);
        Utils.putJson(jobj, "serverid", server);
        Utils.putJson(jobj, "paytype", payType);
        Utils.putJson(jobj, "app_orderid", orderId);
        Utils.putJson(jobj, "subject", subject);
        Utils.putJson(jobj, "subject_id", itemid);
        Utils.putJson(jobj, "body", body);
        Utils.putJson(jobj, "amount", amount);
        Utils.putJson(jobj, "extension", attach);
        Utils.putJson(jobj, "signtype", signType);
        Utils.putJson(jobj, "sign", sign);

        mHttp.post(ALIPAY_PAYINFO_URL, jobj.toString(), "application/json", null, new HttpCallback() {
            @Override
            public void onSuccess(String content) {
                // Log.d(Log.TAG, "content : " + content);
                Response<JSONObject> response = new Response<JSONObject>();
                try {
                    JSONObject jobj = new JSONObject(content);
                    response.setCode(jobj.getString("result_status"));
                    response.setMsg(jobj.getString("msg"));
                    if (jobj.has("biz") && jobj.get("biz") instanceof JSONObject) {
                        response.setObj(jobj.getJSONObject("biz"));
                    }
                } catch (Exception e) {
                    response.setCode(String.valueOf(-1));
                    response.setMsg("json解析失败");
                }
                if (callback != null) {
                    callback.onCallback(response);
                }
            }

            @Override
            public void onFailure(int error, String msg) {
                Response<JSONObject> response = new Response<JSONObject>();
                response.setCode(String.valueOf(error));
                response.setMsg(msg);
                if (callback != null) {
                    callback.onCallback(response);
                }
            }
        });
    }

    @Override
    public void queryShenzhouPay(ResponseCallback<String> callback) {

    }

    @Override
    public void requestChannelOrder(ResponseCallback<Void> callback) {
    }
}
